package net.edu_soft.ec.asset.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.LabelMapper;
import net.edu_soft.ec.asset.pojo.Label;
import net.edu_soft.ec.asset.pojo.LabelExample;
import net.edu_soft.ec.asset.service.LabelService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {

    @Autowired
    private LabelMapper labelMapper;

    @Override
    public Label findById(Integer id) {
        PuingUtil.notNullByZero(id,"标签管理：标签id不能为空或小于等于0");
        return labelMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Label> findList(String name, Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("标签管理：分页参数不能为空或者小于等于0",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        LabelExample example = new LabelExample();
        LabelExample.Criteria criteria = example.createCriteria();
        if(name!=null&&!"".equals(name)){
            //模糊查询
            criteria.andNameLike("%"+name+"%");
        }
        List<Label> labels = labelMapper.selectByExample(example);
        PageInfo<Label> pageInfo = new PageInfo<>(labels);
        return pageInfo;
    }

    @Override
    public int save(String labelName) {
        //设置状态
        Label label = new Label();
        label.setStatus(EduConstant.STATUS_ZREO);
        //根据名称插询是否存在
        //为空则添加
        if(selName(labelName)==null){
            //设置名称
            label.setName(labelName);
            return labelMapper.insertSelective(label);
        }
        return 0;
    }

    @Override
    public int saveList(String labels) {
        int i = 0;
        PuingUtil.isEmpty("标签管理：标签名数据不能为空",labels);
        List<String> labelList = JSONObject.parseArray(labels, String.class);
        for ( String str: labelList) {
            //设置状态
            Label label = new Label();
            label.setStatus(EduConstant.STATUS_ZREO);
            //查询标签名并判断，存在则抛出异常
            PuingUtil.isEmpty("标签管理：标签名不能为空",str);
            if(selName(str)!=null){
                throw new OperationException("标签管理：该标签已存在，禁止重复添加！");
            }
            label.setName(str);
            //添加
            i = labelMapper.insertSelective(label);
        }
        return i;
    }

    @Override
    public int update(Label label) {
        if(label==null){
            throw new ParameterException("标签管理：修改的标签不能为空");
        }
        PuingUtil.notNullByZero(label.getLabelId(),"标签管理：标签id不能为空或小于等于0");
        //根据id查询是否存在，否则抛出
        if(labelMapper.selectByPrimaryKey(label.getLabelId())==null){
            throw new OperationException("标签管理：该标签id查询标签不存在，禁止修改");
        }
        //查询标签名并判断，存在则抛出异常
        if(selName(label.getName())!=null){
            throw new OperationException("标签管理：该标签已存在，修改失败！");
        }
        //修改
        return labelMapper.updateByPrimaryKeySelective(label);
    }

    @Override
    public int updateList(String labels) {
        int i = 0;
        PuingUtil.isEmpty("标签管理：标签数据不能为空",labels);
        List<Label> labelList = JSONObject.parseArray(labels, Label.class);
        PuingUtil.notNullByList(labelList,"标签管理：标签不能为空");
        for ( Label label: labelList) {
            PuingUtil.notNullByZero(label.getLabelId(),"标签管理：标签id不能为空或小于等于0");
            if(labelMapper.selectByPrimaryKey(label.getLabelId())==null){
                throw new OperationException("标签管理：该标签id查询标签不存在，禁止修改");
            }
            //查询标签名并判断，存在则抛出异常
            if(selName(label.getName())!=null){
                throw new OperationException("标签管理：该标签已存在，修改失败！");
            }
            //修改
            i = labelMapper.updateByPrimaryKeySelective(label);
        }

        return i;
    }

    @Override
    public int delList(String ids, Integer isDel) {
        int i = 0;
        if (isDel==EduConstant.IS_DEL){
            //提取id
            Assert.hasLength(ids,"标签管理：id串不能为空");
            List<Integer> idList = PuingUtil.getNumber(ids);
            for (Integer id: idList) {
                PuingUtil.notNullByZero(id,"标签管理：标签id不能为空或小于等于0");
                Label label = labelMapper.selectByPrimaryKey(id);
                //根据id查询是否存在，否则抛出
                if(label==null){
                    throw new OperationException("标签管理：该标签id查询标签不存在，禁止修改");
                }
                //设置状态为不可用 1
                label.setStatus(EduConstant.STATUS_ONE);
                //修改
                i = labelMapper.updateByPrimaryKeySelective(label);
            }
        }
        return i;
    }

    /***
     * 根据名称查询是否存在该标签
     * @param name
     * @return
     */
    public Label selName(String name){
        Assert.hasLength(name,"标签管理：标签名不能为空");
        LabelExample example = new LabelExample();
        LabelExample.Criteria criteria = example.createCriteria();
        //添加名字
        criteria.andNameEqualTo(name);
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<Label> labels = labelMapper.selectByExample(example);
        if(labels.size()>0){
            return labels.get(0);
        }
        return null;
    }

}
